package com.luyuan.crm.member.service.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.Order;

import com.luyuan.crm.member.model.Customer;
import com.luyuan.crm.member.model.Member;
import com.luyuan.crm.member.model.MemberIntegral;
import com.luyuan.crm.member.model.MemberLiveness;
import com.luyuan.crm.member.model.MemberLoyalty;
import com.luyuan.crm.member.service.MemberService;
import com.luyuan.crm.user.Party;
import com.ruijie.components.security.model.AppUser;
import com.ruijie.components.security.model.Role;
import com.ruijie.components.security.service.AppUserService;
import com.ruijie.components.security.service.RoleService;
import com.ruijie.foundation.dao.support.HibernateEntityDao;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaQueryTemplate;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;

public class MemberServiceImpl extends HibernateEntityDao<Member> implements MemberService {

    private AppUserService appUserService;

    private RoleService roleService;

    @Override
    public Member getByName(String name) {
        Member member = this.findUniqueBy("memName", name);
        return member;
    }

    @Override
    public Page pagedQuery(CriteriaTemplate criteria, int page, int pageSize, List<Order> orders) {
        CriteriaQueryTemplate qt = new CriteriaQueryTemplate(getSessionFactory());
        return qt.pagedQuery(criteria, page, pageSize, orders);
    }

    @Override
    public Member createMember(final Customer customer) {
        // 将客户转成会员
        // getHibernateTemplate().executeWithNewSession(new HibernateCallback() {
        // @Override
        // public Object doInHibernate(Session session) throws HibernateException, SQLException {
        // session.beginTransaction();
        updateBySQL("customer.updateCustomerType", new String[] { "member", customer.getId() });
        // session.getTransaction().commit();
        // return null;
        // }
        // });
        evit(customer);

        // 创建帐号
        AppUser user = new AppUser();
        user.setUsername(customer.getMobile());
        user.setPassword("88888888");
        Set<Role> roles = new HashSet<Role>();
        Role role = roleService.findRoleByName("会员");
        roles.add(role);
        user.setRoles(roles);
        appUserService.addUser(user);
        flush();

        // 更新会员资料
        Member member = findUniqueBy("id", customer.getId());

        member.setCreateTime(new Date());
        member.setActiveTime(new Date());
        member.setLiveness(MemberLiveness.COMMONLY);
        member.setLoyalty(MemberLoyalty.INTENTION);
        user = appUserService.findUserByLoginName(user.getUsername());
        member.setAccount(user);
        member.setSupport(null);

        MemberIntegral memberIntegral = new MemberIntegral();
        member.setMemberIntegral(memberIntegral);
        memberIntegral.setMember(member);
        memberIntegral.setCurrentIntegral(0);
        memberIntegral.setDiscount(1);
        memberIntegral.setTotalIntegral(0);
        update(member);

        return member;
    }

    @Override
    public void updateMember(Member member) {
        super.update(member);
    }

    @Override
    public void updateActiveTime(Member member) {
        member.setActiveTime(new Date());
        super.update(member);
    }

    @Override
    public Member getByAccount(AppUser appUser) {
        Member member = findUniqueBy("account", appUser);
        return member;
    }

    @Override
    public Page pagedQueryOnline(CriteriaTemplate criteria, int page, int pageSize, List<Order> orders) {
        CriteriaQueryTemplate qt = new CriteriaQueryTemplate(getSessionFactory());
        return qt.pagedQuery(criteria, page, pageSize, orders);
    }

    public void setAppUserService(AppUserService appUserService) {
        this.appUserService = appUserService;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    @Override
    public List<Member> findByParty(Party party) {
        return findBy("customerService", party);
    }

    @Override
    public Member getById(String id) {
        return super.safeGet(id);
    }

    @Override
    public Member getByMobile(String mobile) {
        return findUniqueBy("mobile", mobile);
    }
}
